from scrapy.spider import Spider
from scrapy.selector import Selector

from ..items import BrouItem, BcuItem


class BrouSpider(Spider):
    name = 'BROU'
    allowed_domains = ["uy.cotizacion-dolar.com"]
    start_urls = ["http://uy.cotizacion-dolar.com/cotizacion_hoy_uruguay.php"]
    
    def parse(self, response):
        """
        BROU
        xpath dolar compra sel.xpath('//table[@title="Cotizaciones"]/tr[2]/td[@class="buy"]/text()')
        xpath dolar venta sel.xpath('//table[@title="Cotizaciones"]/tr[2]/td[@class="sale"]/text()')
        xpath euro compra sel.xpath('//table[@title="Cotizaciones"]/tr[3]/td[@class="buy"]/text()')
        xpath euro venta sel.xpath('//table[@title="Cotizaciones"]/tr[3]/td[@class="sale"]/text()')
        """
        sel = Selector(response)
        item = BrouItem()
        item['dolar_compra'] = sel.xpath('//div[@class="cc-2b"][1]/span[@class="cotizacion-num"]/text()').extract()
        item['dolar_venta'] = sel.xpath('//div[@class="cc-3b"][1]/span[@class="cotizacion-num"]/text()').extract()
        item['euro_compra'] = sel.xpath('//div[@class="cc-2b"][2]/span[@class="cotizacion-num"]/text()').extract()
        item['euro_venta'] = sel.xpath('//div[@class="cc-3b"][2]/span[@class="cotizacion-num"]/text()').extract()

        return item


class BcuSpider(Spider):
    name = 'BCU'
    allowed_domains = ["bcu.gub.uy"]
    start_urls = ["http://bcu.gub.uy/Paginas/Default.aspx"]

    def parse(self, response):
        """
        BCU
        xpath dolar inter sel.xpath('//div[@class="Cotizaciones"]/table/tr[4]/td[3]/text()')
        xpath euro inter sel.xpath('//div[@class="Cotizaciones"]/table/tr[6]/td[3]/text()')
        """
        sel = Selector(response)
        item = BcuItem()
        item['dolar_interbancario'] = sel.xpath('//div[@class="Cotizaciones"]/table/tr[2]/td[3]/text()').extract()
        item['euro_interbancario'] = sel.xpath('//div[@class="Cotizaciones"]/table/tr[4]/td[3]/text()').extract()

        return item
